(function(angular){
    'use strict';
    //创建模块
    var app = angular.module('myApp',[]);
    //创建控制器
    app.controller('demoController',['$scope','$location','$window',function($scope,$location,$window){
        //1:向页面展示数据信息
        // $location.url()可以得到url中锚点值#后的部分
       //$scope.data=[
       //    {id:1,name:"睡觉",completed:true},
       //    {id:2,name:"吃饭",completed:true},
       //    {id:3,name:"背单词",completed:false},
       //    {id:4,name:"洗衣服",completed:true},
       //    {id:5,name:"喝水",completed:false}
       //];
        //封装获取数据的方法
        $scope.getData = function () {
            var str = $window.localStorage.getItem('mydata') || '[]';
            return JSON.parse(str);
        }
        //封装存储数据的方法
        $scope.setData = function () {
            $window.localStorage.setItem('mydata',JSON.stringify($scope.data));
        }
        $scope.data =$scope.getData();
        //2:添加任务
       $scope.newTodo="";
        $scope.add= function () {
            if($scope.newTodo === ""){
                return;
            }
            $scope.data.push({
                id:Math.random(),
                name:$scope.newTodo,
                completed:false
            });
            //置空
            $scope.newTodo="";
            $scope.setData();
        }

        //3:删除任务
      $scope.remove = function (id) {
         for(var i=0;i<$scope.data.length;i++){
             var tmp =$scope.data[i];
             if(tmp.id == id){
                $scope.data.splice(i,1);
                 $scope.setData();
                 return;
             }
         }
      }
        //4:修改任务信息
        $scope.editingId =-1;
        $scope.edit = function (id) {
            $scope.editingId =id;
        }
        $scope.save = function (id) {
            $scope.editingId =-1;
            $scope.setData();
        }
        //5:切换任务完成与否
          $scope.changeStatus=function(){
              $scope.setData();
          }
        //6:批量切换任务完成与否
        $scope.selectAll =false;
        $scope.toggleAll= function () {
            for(var i=0;i<$scope.data.length;i++){
                var temp =$scope.data[i];
               temp.completed =$scope.selectAll;
            }
            $scope.setData();
        }
        //7:显示未完成的任务数
        $scope.getActive = function () {
            var count=0;
            for(var i=0;i<$scope.data.length;i++){
                var temp =$scope.data[i];
                if(!temp.completed){
                    count++;
                }
            }
            return count;
        }
        //8:清除已经完成的任务
        $scope.clearAll = function () {
            for(var i=$scope.data.length-1;i>=0;i--){
                var temp =$scope.data[i];
                if(temp.completed){
                    $scope.data.splice(i,1);
                }
            }
            $scope.setData();
        }
        //9:切换显示不同的任务状态
        //$scope.local =$location;
        $scope.iscompleted ={};
        //$scope.all= function () {
        //     $scope.iscompleted ={}
        //}
        //$scope.active = function () {
        //    $scope.iscompleted={completed :false};
        //}
        //$scope.completed =function(){
        //    $scope.iscompleted ={completed:true};
        //}
        $scope.loca = $location;
        //console.log($location);
        $scope.$watch('loca.url()',function(now,old){
            switch (now){
                case '/active':
                    $scope.iscompleted={completed:false}
                    break;
                case '/completed':
                    $scope.iscompleted ={completed:true}
                    break;
                default:
                    $scope.iscompleted ={};
            }
        });


    }])
})(angular)
